//
//  HomeViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/16.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeView.h"
#import "AddressViewController.h"

#import "TodayHomeViewController.h"
#import "FoundHomeViewController.h"
#import "RecordHomeViewController.h"
#import "ChooseTypeViewController.h"

@interface HomeViewController () <HomeViewDelegate, UIScrollViewDelegate, TodayHomeDelegate, FoundHomeDelegate, RecordHomeViewDelegate>

@property (nonatomic, retain) HomeView *homeView;
@property (nonatomic, retain) UIScrollView *scrollV;
@property (nonatomic, retain) NSMutableArray *homeArr;

@end

@implementation HomeViewController
- (void)dealloc
{
    [_homeView release];
    [_scrollV release];
    [_homeArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.homeArr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
    
    
    
    self.navigationItem.leftBarButtonItem = nil;
    
    [self.navigationController.navigationBar addSubview:self.homeView];
    [_homeView release];
    
    self.scrollV = [[UIScrollView alloc] initWithFrame:self.view.frame];
    self.scrollV.contentSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width * 3, 0);
    // 设置按页滑动
    self.scrollV.pagingEnabled = YES;
    // 取消边界反弹效果
//    self.scrollV.bounces = NO;
    self.scrollV.delegate = self;
    self.scrollV.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    [self.view addSubview:self.scrollV];
    [_scrollV release];

    
    TodayHomeViewController *todayVC = [[TodayHomeViewController alloc] init];
    todayVC.delegate = self;
    [self addChildViewController:todayVC];
    [self.scrollV addSubview:todayVC.view];
    [todayVC release];
    
    FoundHomeViewController *foundVC = [[FoundHomeViewController alloc] init];
    [self addChildViewController:foundVC];
    foundVC.delegate = self;
    CGRect rectFound = foundVC.view.frame;
    rectFound.origin.x = SCREEN_WIDTH;
    foundVC.view.frame = rectFound;
    [self.scrollV addSubview:foundVC.view];
    [foundVC release];
    
    RecordHomeViewController *recordVC = [[RecordHomeViewController alloc] init];
    [self addChildViewController:recordVC];
    recordVC.delegate = self;
    CGRect rectRecord = recordVC.view.frame;
    rectRecord.origin.x = SCREEN_WIDTH * 2;
    recordVC.view.frame = rectRecord;
    [self.scrollV addSubview:recordVC.view];
    [recordVC release];
    
    // 是否第一次启动
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"]) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirst"];
        
        ChooseTypeViewController *chooseVC = [ChooseTypeViewController shareChooseTypeViewController];
        
        [self presentViewController:chooseVC animated:YES completion:^{
            
        }];
    }
    

    
    
    // 接收城市改变通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(homeRefurbish:) name:@"homeRefurbish" object:nil];
    
    // 接收定位成功通知
    [self startLocation];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationHome:) name:@"location" object:nil];
    
}
- (HomeView *)homeView
{
    if (nil == _homeView) {
        
        self.homeView = [[HomeView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
        self.homeView.delegate = self;
    }
    return _homeView;
}
- (void)navigationViewWithIsHidden:(BOOL)isHidden
{
    self.homeView.hidden = isHidden;
}
- (void)locationHome:(NSNotification *)sender
{
    NSString *str = [sender object];
    NSLog(@"%@", str);
    
    NSString *city = [[NSUserDefaults standardUserDefaults] objectForKey:@"cityName"];
    if (![str isEqualToString:city]) {
        
        [self getCity:str];
        

    }
}
#pragma mark - 城市改变通知
- (void)homeRefurbish:(NSNotification *)sender
{
    [self.homeView changeCity];
}
- (void)changeScrollViewWithTag:(NSInteger)tag
{
    [self.scrollV setContentOffset:CGPointMake(tag % 10 * SCREEN_WIDTH, 0) animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger num = self.scrollV.contentOffset.x / SCREEN_WIDTH;
    [self.homeView changeButtonColorWithTag:1000 + num];
}
- (void)addBtnToAddressViews
{
    AddressViewController *addVC = [[AddressViewController alloc] init];
    UINavigationController *navigationC = [[UINavigationController alloc] initWithRootViewController:addVC];
    navigationC.navigationBar.translucent = NO;
    
    // 去掉UINavigationBar下边框
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];//设置背景
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; //覆盖边框阴影
    
    [self presentViewController:navigationC animated:YES completion:^{
        
    }];
    [navigationC release];
    [addVC release];
}
- (void)getCity:(NSString *)cityName
{
    
    NSString *url = [NSString stringWithFormat:@"http://wanapi.damai.cn/citylist.json?source=%d&version=%d", URL_SOURCE, URL_VERSION];
    
    [AppTools getAFNet:url block:^(id result) {
        
        
        NSMutableDictionary *bigDic = result;
        NSMutableArray *bigArr = [bigDic objectForKey:@"data"];
        
        for (NSMutableDictionary *dic in bigArr) {
            
            NSMutableArray *cityArr = [dic objectForKey:@"citylist"];
            
            for (NSMutableDictionary *cityDic in cityArr) {
                
                // 找到定位城市的id
                if ([[cityDic objectForKey:@"name"] isEqualToString:cityName]) {
//                    NSLog(@"%@ -- %@", cityName, [cityDic objectForKey:@"cityid"]);

                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示信息" message:[NSString stringWithFormat:@"检测到您目前正在%@ 是否需要切换城市", cityName] preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                    }];
                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        // 更新城市默认城市id和名称
                        [[NSUserDefaults standardUserDefaults] setObject:cityName forKey:@"cityName"];
                        [[NSUserDefaults standardUserDefaults] setObject:[cityDic objectForKey:@"cityid"] forKey:@"cityid"];
                        // 发送通知 首页刷新
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"homeRefurbish" object:nil];
                        
                    }];
                    
                    [alertC addAction:action1];
                    [alertC addAction:action2];
                    [self presentViewController:alertC animated:YES completion:nil];
                }
            }
        }
        
        
        
        
    } errorBlock:^(NSError *error) {
        
        [self NetworkError:^{
            NSLog(@"网络错误");
        }];
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
